﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Linq;
using Amarok.Agents;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ViewInspector
	{
		private class MyViewAction : ViewAction
		{
		}


		private abstract class TestAbstractClass
		{
			[ViewActionHandler]
			protected abstract Boolean TestMethod3(MyViewAction viewAction);

			[ViewActionHandler]
			private static Boolean TestMethod5(MyViewAction viewAction)
			{
				return true;
			}

			[ViewActionHandler]
			private Boolean TestMethod9(Int32 index, String text)
			{
				return true;
			}
		}

		private class TestBaseClass : TestAbstractClass
		{
			[ViewActionHandler]
			private Boolean TestMethod2(MyViewAction viewAction)
			{
				return true;
			}

			protected override Boolean TestMethod3(MyViewAction viewAction)
			{
				return true;
			}

			[ViewActionHandler]
			private Boolean TestMethod4(MyViewAction viewAction)
			{
				return true;
			}

			[ViewActionHandler]
			private Boolean TestMethod8(Int32 index)
			{
				return true;
			}
		}

		private class TestClass : TestBaseClass
		{
			[ViewActionHandler]
			private Boolean TestMethod1(MyViewAction viewAction)
			{
				return true;
			}

			[ViewActionHandler]
			public String TestMethod6(MyViewAction viewAction)
			{
				return "done";
			}

			[ViewActionHandler]
			public None TestMethod7(MyViewAction viewAction)
			{
				return None.Instance;
			}
		}

		private HandlerInfo FindHandlerInfo(HandlerInfo[] handlerInfos, String methodName)
		{
			return handlerInfos.SingleOrDefault(h => h.MethodInfo.Name.Equals(methodName));
		}


		// --- TESTS ---


		[Test]
		public void Test_Inspect()
		{
			var handlerInfos = ViewInspector.Inspect(typeof(TestClass));
			var handlerInfos2 = ViewInspector.Inspect(typeof(TestClass));

			Assert.That(handlerInfos2, Is.SameAs(handlerInfos));

			var handler1 = FindHandlerInfo(handlerInfos, "TestMethod1");
			Assert.That(handler1, Is.Not.Null);
			Assert.That(handler1.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			var handler2 = FindHandlerInfo(handlerInfos, "TestMethod2");
			Assert.That(handler2, Is.Not.Null);
			Assert.That(handler2.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			var handler3 = FindHandlerInfo(handlerInfos, "TestMethod3");
			Assert.That(handler3, Is.Not.Null);
			Assert.That(handler3.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			var handler4 = FindHandlerInfo(handlerInfos, "TestMethod4");
			Assert.That(handler4, Is.Not.Null);
			Assert.That(handler4.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			var handler5 = FindHandlerInfo(handlerInfos, "TestMethod5");
			Assert.That(handler5, Is.Not.Null);
			Assert.That(handler5.ViewActionType, Is.EqualTo(typeof(MyViewAction)));

			var handler6 = FindHandlerInfo(handlerInfos, "TestMethod6");
			Assert.That(handler6, Is.Null);

			var handler7 = FindHandlerInfo(handlerInfos, "TestMethod7");
			Assert.That(handler7, Is.Null);

			var handler8 = FindHandlerInfo(handlerInfos, "TestMethod8");
			Assert.That(handler8, Is.Null);

			var handler9 = FindHandlerInfo(handlerInfos, "TestMethod9");
			Assert.That(handler9, Is.Null);
		}

	}
}

#endif
